// components/dd-acti/dd-acti-cnfm/index.js
import Toast from '@vant/weapp/toast/toast';
import Dialog from '@vant/weapp/dialog/dialog';
import Notify from '@vant/weapp/notify/notify';
var { getFileExtension } = require('../../../pages/utils/file');
var dayjs = require('dayjs');
Component({

  /**
   * 组件的属性列表
   */
  properties: {
    formData: { type: Object, value: {}, observer: 'updateFormData', },
  },

  /**
   * 组件的初始数据
   */
  data: {
    CustomBar: getApp().globalData.CustomBar,
    form: {
      // 活动序号
      actiId: 0,
      // 活动标题
      actiTitle: '',
      // 活动日期
      actiDate: dayjs().format('YYYYMMDD'),
      // 活动日期展示
      actiDate_label: dayjs().format('YYYY-MM-DD'),
      // 活动开始时间
      actiBegnTime: '1900',
      // 活动开始时间展示
      actiBegnTime_label: '19:00',
      // 活动结束时间
      actiEndTime: '2000',
      // 活动结束时间展示
      actiEndTime_label: '20:00',
      // 注意事项
      actiMemo: '',
      // 活动图片
      fileList: [],
      // 活动图片文件序号列表
      actiFileIdList: '',
      // 商品列表
      actiPrdList: [
        {
          // 商品折叠面板标题
          title: "商品1",
          // 商品折叠面板名称
          name: "1",
          // 临时-商品图片列表
          fileList: [],
          // 商品详情
          prdInfo: {
            // 商品序号
            prdId: 0,
            // 商品名称
            prdName: '',
            // 活动方式-多层级活动开关
            currActiModeSwitch: false,
            // 活动方式列表
            actiModeList: [],
            // 活动方式列表展示
            actiModeList_label: '',
            // 活动时间列表展示
            actiTimeList_label: '',
            // 商品简介
            prdContent: '',
            // 商品价格
            prdPrice: '',
            // 商品数量
            prdVol: '',
            // 商品工期
            prdDays: '',
            // 商品点数
            prdPointCount: '',
            // 商品文件序号列表
            prdFileIdList: '',
            // 正式-商品图片列表
            fileList: [],
            // 加购项列表
            actiGoodsTypeList: [],
            // 加购项展示
            actiGoodsTypeList_label: '',
          },
        }
      ],
    },
    // 活动日期-展示弹出层标识
    showActiDate: false,
    // 活动时间-展示弹出层标识
    showActiTime: false,
    // 活动时间-弹出层控件绑定值
    currActiTime: '19:00',
    // 活动时间-弹出层控件修改时间类型（1-开始时间、2-结束时间、3-活动方式开始时间、4-活动方式结束时间）
    currActiTimeType: 1,
    // 活动时间-弹出层控件展示样式
    formatterTime(type, value) {
      if (type === 'hour') {
        return `${value} 时`;
      }
      if (type === 'minute') {
        return `${value} 分`;
      }
      return value;
    },
    // 活动时间-弹出层控件过滤项
    filterTime(type, options) {
      if (type === 'minute') {
        return options.filter((option) => option % 5 === 0);
      }
      return options;
    },
    // 当前商品索引
    currPrdIndex: 1,
    // 当前展开商品的名称
    currPrdName: "1",
    // 上传图片中当前展开商品的名称
    currPrdNameUpload: "1",
    // 活动方式-展示弹出层标识
    showActiMode: false,
    // 活动方式-商品合计
    totalPrdVol: 0,
    // 活动方式-多层级活动-开关
    currActiModeSwitch: false,
    // 活动方式-多层级活动-弹出层集合
    currActiModeMap: new Map(),
    // 活动方式-多层级活动-当前折叠框
    currActiModeMapKey: "1",
    // 活动方式-多层级活动-折叠框列表
    actiModeMapKeyList: [{
      title: '第一轮活动',
      name: '1',
    }, {
      title: '第二轮活动',
      name: '2',
    }, {
      title: '第三轮活动',
      name: '3',
    }],
    // 活动方式-弹出层列表
    currActiModeList: [
      {
        actiMode: 1,
        actiModeType: 1,
        actiModeName: "抽奖",
        prdVol: 0,
      },
      {
        actiMode: 2,
        actiModeType: 1,
        actiModeName: "摇号",
        prdVol: 0,
      },
      {
        actiMode: 3,
        actiModeType: 1,
        actiModeName: "私养图",
        prdVol: 0,
      },
      {
        actiMode: 4,
        actiModeType: 1,
        actiModeName: "拼手速",
        prdVol: 0,
      },
      {
        actiMode: 5,
        actiModeType: 1,
        actiModeName: "黑箱卡",
        prdVol: 0,
      },
    ],
    // 活动时间-展示弹出层标识
    showActiModeTime: false,
    // 活动时间-当前活动方式值
    currActiMode: null,
    // 发布活动loading状态
    submitLoading: false,
    // 上传图片弹框-展示标识
    showUpload: false,
  },

  /**
   * 组件的方法列表
   */
  methods: {
    updateFormData(newValue) {
      if (JSON.stringify(newValue) != "{}") {
        this.setData({ form: newValue })
      }
    },
    // 活动日期-弹出层展示
    onShowActiDate() {
      this.setData({ showActiDate: true });
    },
    // 活动日期-弹出层关闭或者点击取消
    onCloseActiDate() {
      this.setData({ showActiDate: false });
    },
    // 活动日期-弹出层点击确认
    onConfirmActiDate(event) {
      this.setData({
        showActiDate: false,
        'form.actiDate': dayjs(event.detail).format('YYYYMMDD'),
        'form.actiDate_label': dayjs(event.detail).format('YYYY-MM-DD'),
      });
    },
    // 打开时间选择器-开始时间、结束时间
    onShowActiTime(event) {
      // 将form中的开始结束时间 回填至currActiTime
      let currActiTime = "";
      const currActiTimeType = event.currentTarget.dataset.operation;
      if (currActiTimeType == 1) {
        currActiTime = this.data.form.actiBegnTime_label;
      } else if (currActiTimeType == 2) {
        currActiTime = this.data.form.actiEndTime_label;
      }
      this.setData({
        showActiTime: true,
        currActiTime: currActiTime,
        currActiTimeType: currActiTimeType
      });
    },
    // 关闭时间选择器
    onCloseActiTime() {
      this.setData({ showActiTime: false });
    },
    // 时间选择器-点击确认
    onConfirmActiTime(event) {
      // 将选择好的时间回填至currActiModeList
      if (this.data.currActiTimeType == 1) {
        this.setData({
          'form.actiBegnTime': event.detail.replace(":", ""),
          'form.actiBegnTime_label': event.detail,
          showActiTime: false
        });
      } else if (this.data.currActiTimeType == 2) {
        this.setData({
          'form.actiEndTime': event.detail.replace(":", ""),
          'form.actiEndTime_label': event.detail,
          showActiTime: false
        });
      } else if (this.data.currActiTimeType == 3) {
        for (const item of this.data.currActiModeList) {
          if (item.actiMode == this.data.currActiMode) {
            item.actiBegnTime = event.detail.replace(":", "");
            item.actiBegnTime_label = event.detail;
          }
        }
        this.setData({
          currActiModeList: this.data.currActiModeList,
          showActiTime: false
        });
      } else if (this.data.currActiTimeType == 4) {
        for (const item of this.data.currActiModeList) {
          if (item.actiMode == this.data.currActiMode) {
            item.actiEndTime = event.detail.replace(":", "");
            item.actiEndTime_label = event.detail;
          }
        }
        this.setData({
          currActiModeList: this.data.currActiModeList,
          showActiTime: false
        });
      }
    },
    // 活动方式-弹出层展示
    onShowActiMode(event) {
      const key = event.currentTarget.dataset.operation;
      const actiModeList = this.data.form.actiPrdList[key].prdInfo.actiModeList;
      const currActiModeSwitch = this.data.form.actiPrdList[key].prdInfo.currActiModeSwitch;
      for (const key of this.data.currActiModeMap.keys()) {
        const currActiModeList = this.data.currActiModeMap.get(key);
        for (const item of currActiModeList) {
          item.prdVol = 0;
          item.actiBegnTime = this.data.form.actiBegnTime;
          item.actiBegnTime_label = this.data.form.actiBegnTime_label;
          item.actiEndTime = this.data.form.actiEndTime;
          item.actiEndTime_label = this.data.form.actiEndTime_label;
          if (actiModeList != null && actiModeList.length > 0) {
            const tmp = actiModeList.filter(e => { return e.actiMode == item.actiMode && e.actiModeLevel == key })
            if (tmp != null && tmp.length > 0) {
              item.prdVol = tmp[0].prdVol;
              item.actiBegnTime = tmp[0].actiBegnTime;
              item.actiBegnTime_label = tmp[0].actiBegnTime_label;
              item.actiEndTime = tmp[0].actiEndTime;
              item.actiEndTime_label = tmp[0].actiEndTime_label;
            }
          }
        }
        this.data.currActiModeMap.set(key, currActiModeList);
      }
      this.setData({
        showActiMode: true,
        currActiModeMap: this.data.currActiModeMap,
        currActiModeList: this.data.currActiModeMap.get("1"),
        totalPrdVol: this.getTotalPrdVol(),
        currActiModeSwitch: currActiModeSwitch,
        currActiModeMapKey: "1",
      });
    },
    // 活动方式-弹出层关闭或者点击确认
    onCloseActiMode() {
      let actiModeList = [];
      let actiModeList_label = "";
      for (const key of this.data.currActiModeMap.keys()) {
        const currActiModeList = this.data.currActiModeMap.get(key);
        for (const item of currActiModeList) {
          if (item.actiModeName != '' && item.prdVol > 0) {
            actiModeList.push({
              actiMode: item.actiMode,
              actiModeLevel: key,
              actiModeType: item.actiModeType,
              actiModeName: item.actiModeName,
              prdVol: item.prdVol,
              actiBegnTime: item.actiBegnTime,
              actiBegnTime_label: item.actiBegnTime_label,
              actiEndTime: item.actiEndTime,
              actiEndTime_label: item.actiEndTime_label,
            });
            actiModeList_label += item.actiModeName + item.prdVol + '件；';
          }
        }
      }
      for (const actiPrd of this.data.form.actiPrdList) {
        if (this.data.currPrdName == actiPrd.name) {
          actiPrd.prdInfo.currActiModeSwitch = this.data.currActiModeSwitch;
          actiPrd.prdInfo.actiModeList = actiModeList;
          actiPrd.prdInfo.actiModeList_label = actiModeList_label;
        }
      }
      this.setData({ showActiMode: false, 'form.actiPrdList': this.data.form.actiPrdList });
    },
    // 获取商品总数
    getTotalPrdVol() {
      let totalPrdVol1 = 0;
      let totalPrdVol2 = 0;
      let totalPrdVol3 = 0;
      for (const key of this.data.currActiModeMap.keys()) {
        const currActiModeList = this.data.currActiModeMap.get(key);
        for (const item of currActiModeList) {
          if (key == 1) {
            totalPrdVol1 += Number(item.prdVol);
          } else if (key == 2) {
            totalPrdVol2 += Number(item.prdVol);
          } else if (key == 3) {
            totalPrdVol3 += Number(item.prdVol);
          }
        }
      }
      if (totalPrdVol3) return totalPrdVol3;
      if (totalPrdVol2) return totalPrdVol2;
      if (totalPrdVol1) return totalPrdVol1;
      return 0;
    },
    // 活动方式-弹出层的商品数量变更
    onChangeActiMode(event) {
      const actiModeKey = event.currentTarget.dataset.operation;
      this.data.currActiModeList[actiModeKey].prdVol = event.detail;
      const totalPrdVol = this.getTotalPrdVol();
      for (const actiPrd of this.data.form.actiPrdList) {
        if (this.data.currPrdName == actiPrd.name) {
          actiPrd.prdInfo.prdVol = totalPrdVol;
        }
      }
      this.setData({ currActiModeList: this.data.currActiModeList, totalPrdVol: totalPrdVol, 'form.actiPrdList': this.data.form.actiPrdList });
    },
    // 活动方式-弹出层的活动方式名称变更
    onChangeActiModeName(event) {
      const actiModeKey = event.currentTarget.dataset.operation;
      this.data.currActiModeList[actiModeKey].actiModeName = event.detail;
      this.setData({ 'actiPrdList': this.data.currActiModeList });
    },
    // 活动方式-弹出层的多层级活动变更
    onChangeToSwitch(event) {
      const currActiModeSwitch = event.detail.value;
      this.setData({
        currActiModeSwitch: currActiModeSwitch,
        currActiModeMapKey: "1",
        currActiModeList: this.data.currActiModeMap.get("1"),
      })
    },
    // 活动方式-弹出层的多层级活动折叠款变更
    onActiModeMapChange(event) {
      // 更新关闭折叠框的currActiModeMap
      // 回显打开折叠框的currActiModeList
      if (this.data.currActiModeMapKey) {
        this.data.currActiModeMap.set(this.data.currActiModeMapKey, this.data.currActiModeList);
        this.setData({
          currActiModeMapKey: event.detail,
          currActiModeMap: this.data.currActiModeMap,
          currActiModeList: this.data.currActiModeMap.get(event.detail),
        })
      }
    },
    // 添加自定义活动
    onClickToAddActiMode() {
      if (this.data.currActiModeList.length < 10) {
        this.data.currActiModeList.push({
          actiMode: this.data.currActiModeList.length + 1,
          actiModeType: 2,
          actiModeName: '',
          prdVol: 0,
          actiBegnTime: this.data.form.actiBegnTime,
          actiBegnTime_label: this.data.form.actiBegnTime_label,
          actiEndTime: this.data.form.actiEndTime,
          actiEndTime_label: this.data.form.actiEndTime_label,
        });
        this.setData({
          currActiModeList: this.data.currActiModeList
        })
      } else {
        Toast('活动方式最多支持10个！');
      }
    },
    // 活动方式时间-弹出层展示
    onShowActiModeTime(event) {
      // 将form中的数据回填到currActiModeList
      const key = event.currentTarget.dataset.operation;
      const actiModeList = this.data.form.actiPrdList[key].prdInfo.actiModeList;
      for (const key of this.data.currActiModeMap.keys()) {
        const currActiModeList = this.data.currActiModeMap.get(key);
        for (const item of currActiModeList) {
          if (actiModeList != null && actiModeList.length > 0) {
            item.prdVol = 0;
            item.actiBegnTime = this.data.form.actiBegnTime;
            item.actiBegnTime_label = this.data.form.actiBegnTime_label;
            item.actiEndTime = this.data.form.actiEndTime;
            item.actiEndTime_label = this.data.form.actiEndTime_label;
            const tmp = actiModeList.filter(e => { return e.actiMode == item.actiMode && e.actiModeLevel == key })
            if (tmp != null && tmp.length > 0) {
              item.prdVol = tmp[0].prdVol;
              item.actiBegnTime = tmp[0].actiBegnTime;
              item.actiBegnTime_label = tmp[0].actiBegnTime_label;
              item.actiEndTime = tmp[0].actiEndTime;
              item.actiEndTime_label = tmp[0].actiEndTime_label;
            }
          }
        }
        this.data.currActiModeMap.set(key, currActiModeList);
      }
      this.setData({
        showActiModeTime: true,
        currActiModeMap: this.data.currActiModeMap,
        currActiModeList: this.data.currActiModeMap.get("1"),
        currActiModeMapKey: "1",
      });
    },
    // 活动方式时间-弹出层关闭或者点击确认
    onCloseActiModeTime() {
      // 将currActiModeList中的数据回填到form
      let actiModeList = [];
      let actiTimeList_label = "";
      for (const key of this.data.currActiModeMap.keys()) {
        const currActiModeList = this.data.currActiModeMap.get(key);
        for (const item of currActiModeList) {
          if (item.actiModeName != '' && item.prdVol > 0) {
            actiModeList.push({
              actiMode: item.actiMode,
              actiModeLevel: key,
              actiModeType: item.actiModeType,
              actiModeName: item.actiModeName,
              prdVol: item.prdVol,
              actiBegnTime: item.actiBegnTime,
              actiBegnTime_label: item.actiBegnTime_label,
              actiEndTime: item.actiEndTime,
              actiEndTime_label: item.actiEndTime_label,
            });
            if (item.actiMode == 1) {
              actiTimeList_label += item.actiModeName + '：' + item.actiBegnTime_label + '开奖；';
            } else {
              actiTimeList_label += item.actiModeName + '：' + item.actiBegnTime_label + '~' + item.actiEndTime_label + '；';
            }
          }
        }
      }
      for (const actiPrd of this.data.form.actiPrdList) {
        if (this.data.currPrdName == actiPrd.name) {
          actiPrd.prdInfo.actiModeList = actiModeList;
          actiPrd.prdInfo.actiTimeList_label = actiTimeList_label;
        }
      }
      this.setData({ showActiModeTime: false, 'form.actiPrdList': this.data.form.actiPrdList });
    },
    // 打开时间选择器-活动方式开始时间、活动方式结束时间
    onSelectActiModeTime(event) {
      const data = event.currentTarget.dataset.operation;
      // 回显已选择的时间至currActiTime
      let currActiTime = "";
      const tmp = this.data.currActiModeList.filter(e => e.actiMode == data.actiMode);
      if (tmp != null) {
        if (data.currActiTimeType == 3) {
          currActiTime = tmp[0].actiBegnTime_label;
        } else if (data.currActiTimeType == 4) {
          currActiTime = tmp[0].actiEndTime_label;
        }
      }
      this.setData({
        showActiTime: true,
        currActiTimeType: data.currActiTimeType,
        currActiMode: data.actiMode,
        currActiTime: currActiTime
      });
    },
    // 商品列表展开
    onActiveNameChange(event) {
      this.setData({
        currPrdName: event.detail,
      });
    },
    // 上传图片中商品列表展开
    onActiveNameChangeUpload(event) {
      this.setData({
        currPrdNameUpload: event.detail,
      });
    },
    // 添加商品
    onClickToAddPrd() {
      const currPrdIndex = this.data.currPrdIndex + 1;
      this.data.form.actiPrdList.push({
        title: "商品" + currPrdIndex,
        name: "" + currPrdIndex,
        fileList: [],
        prdInfo: {},
      });
      this.setData({ 'form.actiPrdList': this.data.form.actiPrdList, currPrdName: "" + currPrdIndex, currPrdIndex });
    },
    // 删除商品
    onCloseToDelPrd(event) {
      const { position, instance } = event.detail;
      switch (position) {
        case 'left':
        case 'cell':
          instance.close();
          break;
        case 'right':
          if (this.data.form.actiPrdList?.length <= 1) {
            Toast('商品不得全部删除！');
            instance.close();
            break;
          }
          Dialog.confirm({
            message: '确定删除商品吗？',
          }).then(() => {
            const newPrdDataList = this.data.form.actiPrdList.filter(item => { return item.name != event.detail.name });
            this.setData({ 'form.actiPrdList': newPrdDataList, currPrdName: null });
            instance.close();
          }).catch(() => { });
          break;
      }
    },
    // 活动标题-数据绑定
    onChangeToUpdActiTitle(event) {
      this.setData({ 'form.actiTitle': event.detail });
    },
    // 注意事项-数据绑定
    onChangeToUpdActiMemo(event) {
      this.setData({ 'form.actiMemo': event.detail });
    },
    // 商品名称-数据绑定
    onChangeToUpdPrdName(event) {
      const id = event.currentTarget.dataset.operation;
      if (event.detail != '') {
        this.data.form.actiPrdList[id].title = '商品：' + event.detail;
      } else {
        this.data.form.actiPrdList[id].title = '商品' + this.data.form.actiPrdList[id].name;
      }
      this.data.form.actiPrdList[id].prdInfo.prdName = event.detail;
      this.setData({ 'form.actiPrdList': this.data.form.actiPrdList });
    },
    // 商品简介-数据绑定
    onChangeToUpdPrdContent(event) {
      const id = event.currentTarget.dataset.operation;
      this.data.form.actiPrdList[id].prdInfo.prdContent = event.detail;
      this.setData({ 'form.actiPrdList': this.data.form.actiPrdList });
    },
    // 商品价格-数据绑定
    onChangeToUpdPrdPrice(event) {
      const id = event.currentTarget.dataset.operation;
      this.data.form.actiPrdList[id].prdInfo.prdPrice = event.detail;
      this.setData({ 'form.actiPrdList': this.data.form.actiPrdList });
    },
    // 商品数量-数据绑定
    onChangeToUpdPrdVol(event) {
      const id = event.currentTarget.dataset.operation;
      this.data.form.actiPrdList[id].prdInfo.prdVol = event.detail;
      this.setData({ 'form.actiPrdList': this.data.form.actiPrdList });
    },
    // 商品工期-数据绑定
    onChangeToUpdPrdDays(event) {
      const id = event.currentTarget.dataset.operation;
      this.data.form.actiPrdList[id].prdInfo.prdDays = event.detail;
      this.setData({ 'form.actiPrdList': this.data.form.actiPrdList });
    },
    // 商品点数-数据绑定
    onChangeToUpdPrdPointCount(event) {
      const id = event.currentTarget.dataset.operation;
      this.data.form.actiPrdList[id].prdInfo.prdPointCount = event.detail;
      this.setData({ 'form.actiPrdList': this.data.form.actiPrdList });
    },
    // 打开上传图片弹框
    onClickToUpload() {
      this.setData({ showUpload: true });
    },
    // 关闭上传图片弹框
    onCloseUpload() {
      this.setData({ showUpload: false });
    },
    // 跳转至商品选择页
    onShowPrdGoods(event) {
      const prdIndex = event.currentTarget.dataset.operation;
      const actiGoodsTypeList = this.data.form.actiPrdList[prdIndex].prdInfo.actiGoodsTypeList;
      let goodsIdList = [];
      for (const goodsType of actiGoodsTypeList) {
        for (const goods of goodsType.actiPrdGoodsInfoAddDTOList) {
          goodsIdList.push(goods.goodsId);
        }
      }
      wx.navigateTo({
        url: '/pages/store/storegoods/goodsselect/goodsselect?goodsIdList=' + JSON.stringify(goodsIdList),
        events: {
          // 为指定事件添加一个监听器，获取被打开页面传送到当前页面的数据
          acceptDataFromGoodsSelect: (data) => {
            let goodsCount = 0;
            for (const item of data) {
              goodsCount += item.actiPrdGoodsInfoAddDTOList.length;
            }
            this.data.form.actiPrdList[prdIndex].prdInfo.actiGoodsTypeList_label = '已选' + goodsCount + '件';
            this.data.form.actiPrdList[prdIndex].prdInfo.actiGoodsTypeList = data;
            this.setData({ 'form.actiPrdList': this.data.form.actiPrdList });
          },
        },
      })
    },
    // 校验参数
    checkParam() {
      const formData = this.data.form;
      if (formData.actiTitle == '') {
        Toast('请输入活动标题！');
        return false;
      }
      if (formData.actiEndTime < formData.actiBegnTime) {
        Toast('结束时间必须大于等于开始时间！');
        return false;
      }
      for (const actiPrd of formData.actiPrdList) {
        if (actiPrd.prdInfo.prdName == '') {
          Toast('请输入' + actiPrd.title + '的商品名称！');
          return false;
        } else if (actiPrd.prdInfo.actiModeList == null || actiPrd.prdInfo.actiModeList.length == 0) {
          Toast('请选择' + actiPrd.title + '的活动方式！');
          return false;
        } else if (actiPrd.prdInfo.prdPrice == '') {
          Toast('请输入' + actiPrd.title + '的商品价格！');
          return false;
        } else if (isNaN(Number(actiPrd.prdInfo.prdPrice))) {
          Toast(actiPrd.title + '的商品价格格式不正确！');
          return false;
        } else if (actiPrd.prdInfo.prdVol == '') {
          Toast('请输入' + actiPrd.title + '的商品数量！');
          return false;
        } else if (isNaN(Number(actiPrd.prdInfo.prdVol))) {
          Toast(actiPrd.title + '的商品数量格式不正确！');
          return false;
        } else if (!actiPrd.prdInfo.prdFileIdList || actiPrd.prdInfo.prdFileIdList.length == 0) {
          Toast('请上传' + actiPrd.title + '的图片！');
          return false;
        }
      }
      return true;
    },
    // 转换参数
    transParam() {
      const formData = this.data.form;
      let actiPrdInfoDTOList = [];
      for (const actiPrd of formData.actiPrdList) {
        let actiModeMaxLevel = 1;
        for (const actiMode of actiPrd.prdInfo.actiModeList) {
          if (actiModeMaxLevel < actiMode.actiModeLevel) {
            actiModeMaxLevel = actiMode.actiModeLevel;
          }
        }
        actiPrdInfoDTOList.push({
          prdId: actiPrd.prdInfo.prdId,
          prdName: actiPrd.prdInfo.prdName,
          prdContent: actiPrd.prdInfo.prdContent,
          prdPrice: actiPrd.prdInfo.prdPrice,
          prdVol: actiPrd.prdInfo.prdVol,
          prdDays: actiPrd.prdInfo.prdDays,
          prdPointCount: actiPrd.prdInfo.prdPointCount,
          prdFileIdList: actiPrd.prdInfo.prdFileIdList,
          actiModeMaxLevel: actiModeMaxLevel,
          actiPrdModeInfoDTOList: actiPrd.prdInfo.actiModeList || [],
          actiPrdGoodsTypeInfoAddDTOList: actiPrd.prdInfo.actiGoodsTypeList || [],
        });
      }
      return {
        actiId: formData.actiId,
        actiTitle: formData.actiTitle,
        actiDate: formData.actiDate,
        actiBegnTime: formData.actiBegnTime,
        actiEndTime: formData.actiEndTime,
        actiMemo: formData.actiMemo,
        actiFileIdList: formData.actiFileIdList,
        storeId: getApp().globalData.userInfo.storeId,
        storeName: getApp().globalData.userInfo.storeName,
        actiPrdInfoDTOList: actiPrdInfoDTOList,
      }
    },
    // 发布活动提交
    onClickToSubmit() {
      if (this.data.submitLoading) {
        return;
      }
      if (!this.checkParam()) {
        this.setData({ submitLoading: false });
        return;
      }
      wx.requestSubscribeMessage({
        tmplIds: ['pATN40-Jqv75gFva6r7sBKwkaXgHA3qBZynpW8C1Qtg'],
        success: (res) => {
          this.setData({ submitLoading: true });
          const param = this.transParam();
          let url = "/acti/add";
          if (this.data.form.actiId) {
            url = "/acti/upd";
          }
          getApp().callContainer(url, param, (res) => {
            if (res.data.code == 0) {
              this.setData({ submitLoading: false });
              Notify({ type: 'success', message: '发布成功！', top: this.data.CustomBar });
              setTimeout(() => {
                wx.navigateBack({
                  delta: 1
                })
              }, 500);
            } else {
              this.setData({ submitLoading: false });
              Notify({ type: 'danger', message: '发布失败！', top: this.data.CustomBar });
            }
          }, (res) => {
            this.setData({ submitLoading: false });
          })
        },
      });
    },
    // 文件选择后
    afterRead(event) {
      const prdIndex = event.currentTarget.dataset.operation;
      const fileList = event.detail.file;
      // 组装文件前缀
      let index = 0;
      for (const file of fileList) {
        const currDate = new Date().toLocaleDateString().replaceAll('/', '-');
        const userId = getApp().globalData.userInfo.userId;
        const prdTitle = this.data.form.actiPrdList[prdIndex].title;
        const filePath = 'acti_picture/' + currDate + '/' + userId + '/';
        const fileName = prdTitle + '-' + index + '-' + new Date().getTime() + '.' + getFileExtension(file.url);
        file.fileName = filePath + fileName;
        index++;
      }
      // 调用文件上传接口
      const uploadTasks = fileList.map((file) => getApp().uploadFilePromise(file.fileName, file));
      Promise.all(uploadTasks)
        .then(data => {
          Toast('上传成功');
          const newFileList = data.map(item => ({ url: item.fileID }));
          for (const newFile of newFileList) {
            this.data.form.actiPrdList[prdIndex].fileList.push(newFile);
          }
          this.setData({ 'form.actiPrdList': this.data.form.actiPrdList });
        })
        .catch(e => {
          Toast('上传失败');
          console.log(e);
        });
    },
    // 删除图片
    deleteFile(event) {
      const prdIndex = event.currentTarget.dataset.operation;
      this.data.form.actiPrdList[prdIndex].fileList.splice(event.detail.index, 1);
      this.setData({ 'form.actiPrdList': this.data.form.actiPrdList });
    },
    // 上传图片-确定
    onConfirmUpload() {
      for (const actiPrd of this.data.form.actiPrdList) {
        const fileIdArr = actiPrd.fileList.map(e => (e.url));
        actiPrd.prdInfo.prdFileIdList = fileIdArr.join(',');
        actiPrd.prdInfo.fileList = JSON.parse(JSON.stringify(actiPrd.fileList));
      }
      this.setData({ 'form.actiPrdList': this.data.form.actiPrdList });
    },
    // 上传图片-关闭
    onCloseUpload(event) {
      if (event.detail == 'confirm') {
        return;
      }
      for (const actiPrd of this.data.form.actiPrdList) {
        actiPrd.fileList = JSON.parse(JSON.stringify(actiPrd.prdInfo.fileList));
      }
      this.setData({ 'form.actiPrdList': this.data.form.actiPrdList });
    },
    // 上一步
    onClickToLastStep() {
      this.triggerEvent('lastStep', this.data.form);
    },
  },
  lifetimes: {
    attached: function () {
      // 在组件实例进入页面节点树时执行
      const currActiModeMap = new Map();
      currActiModeMap.set("1", JSON.parse(JSON.stringify(this.data.currActiModeList)));
      currActiModeMap.set("2", JSON.parse(JSON.stringify(this.data.currActiModeList)));
      currActiModeMap.set("3", JSON.parse(JSON.stringify(this.data.currActiModeList)));
      this.setData({ currActiModeMap: currActiModeMap });
      if (JSON.stringify(this.properties.formData) != "{}") {
        this.setData({ form: this.properties.formData })
      }
    },
    detached: function () {
      // 在组件实例被从页面节点树移除时执行
    },
  },
})